package com.pine.manager.pineframework.trigger.impl;

import cn.hutool.json.JSONUtil;
import com.pine.common.beans.pineframework.request.DynamicThreadPoolRefreshRequest;
import com.pine.common.threadpool.ThreadPoolConfig;
import com.pine.common.threadpool.ThreadPoolConstant;
import com.pine.common.threadpool.core.ThreadPoolConfigFactory;
import com.pine.common.trigger.TriggerContext;
import com.pine.dao.pineframework.entity.SysDict;
import com.pine.manager.core.StringPool;
import com.pine.manager.core.TriggerTypeEnum;
import com.pine.manager.pineframework.bo.SysDictBo;
import com.pine.manager.pineframework.bo.SysDictItemBo;
import com.pine.manager.pineframework.query.SysDictItemQuery;
import com.pine.manager.pineframework.trigger.AbstractTriggerTemplateAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 动态线程池参数刷新 触发器
 * </p>
 *
 * @author pine manage
 * @since 2024-08-29
 */
@Component
@Slf4j
public class DynamicThreadPoolRefreshTrigger extends AbstractTriggerTemplateAdapter<DynamicThreadPoolRefreshRequest, Boolean> {

    public DynamicThreadPoolRefreshTrigger() {
        super(TriggerTypeEnum.DYNAMIC_THREAD_POOL_REFRESH);
    }


    /**
     * 触发
     *
     * @param triggerContext 触发上下文
     * @return 触发结果
     */
    @Override
    public Boolean trigger(TriggerContext<DynamicThreadPoolRefreshRequest> triggerContext) {
        // 根据code查字典
        SysDictBo sysDictBo = sysDictManager.getOneByUniqueKey(StringPool.DYNAMIC_THREAD_POOL_CONFIG_DICT_CODE, SysDict::getCode);
        if (sysDictBo == null) {
            log.warn("线程池配置字典编码{}不存在", StringPool.DYNAMIC_THREAD_POOL_CONFIG_DICT_CODE);
            return false;
        }

        DynamicThreadPoolRefreshRequest request = triggerContext.getTriggerRequest();
        // 查字典明细
        SysDictItemQuery sysDictItemQuery = new SysDictItemQuery();
        sysDictItemQuery.setDictId(sysDictBo.getId());
        sysDictItemQuery.setName(ThreadPoolConstant.THREAD_POOL_NAME_FORMAT.formatted(request.getBusinessCode(), request.getBusinessKey()));
        SysDictItemBo sysDictItemBo = sysDictItemManager.getOne(sysDictItemQuery);
        if (sysDictItemBo == null) {
            log.warn("线程池配置字典项名称{}不存在", sysDictItemQuery.getName());
            return false;
        }

        // 线程池配置json，例如：{"maximumPoolSize":10,"corePoolSize":5,"keepAliveTime":60,"keepAliveTimeUnit":"SECONDS","blockingQueueCapacity":1000,"rejectedPolicyCode":"DiscardPolicy"}
        String configValueJson = sysDictItemBo.getValue();
        ThreadPoolConfig threadPoolConfig = JSONUtil.toBean(configValueJson, ThreadPoolConfig.class);
        ThreadPoolConfig oldConfig = ThreadPoolConfigFactory.getThreadPoolConfig(request.getBusinessCode(), request.getBusinessKey());
        if (oldConfig == null) {
            log.warn("线程池配置{}##{}不存在", request.getBusinessCode(), request.getBusinessKey());
            return false;
        }
        // 增加线程池配置
        ThreadPoolConfigFactory.addThreadPoolConfig(request.getBusinessCode(), request.getBusinessKey(), threadPoolConfig);
        log.info("扫描到线程池(configKey={}##{})配置信息：{}", request.getBusinessCode(), request.getBusinessKey(), threadPoolConfig);
        return true;
    }
}
